package com.durian.ui.factory;

import android.app.Activity;
import android.app.Dialog;
import android.graphics.drawable.Drawable;
import androidx.annotation.DrawableRes;
import androidx.annotation.Size;
import androidx.annotation.StringRes;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.TextView;

/**
 * 说明：ViewTools
 * <p/>
 * 作者：fanly
 * <p/>
 * 类型：Class
 * <p/>
 * 时间：2019-06-18 14:53
 * <p/>
 * 版本：version 1.0
 */
public class ViewTools {
    public interface Direction{
        int Left = 0;
        int Right = 1;
        int Top = 2;
        int Bottom = 3;
    }

    /**
     * 说明：禁止实例化
     */
    private ViewTools(){}

    public static void setImageResource(ImageView iv,@DrawableRes int res){
        if (iv != null && res != 0){
            iv.setImageResource(res);
        }
    }

    /**
     * 说明：View设置颜色
     *          ---TextView
     *          ---Button
     *          ---CheckBox
     *          ---EditText
     *          ---RadioButton
     * @param v
     * @param color
     */
    public static void setTextColor(View v, int color){
        if (v == null){
            return;
        }
        if (v instanceof TextView){
            ((TextView)v).setTextColor(color);
        }else if (v instanceof Button){
            ((Button)v).setTextColor(color);
        }else if (v instanceof CheckBox){
            ((CheckBox)v).setTextColor(color);
        }else if (v instanceof EditText){
            ((EditText)v).setTextColor(color);
        }else if (v instanceof RadioButton){
            ((RadioButton)v).setTextColor(color);
        }
    }

    /**
     * 说明：View赋值
     *          ---TextView
     *          ---Button
     *          ---CheckBox
     *          ---EditText
     *          ---RadioButton
     * @param v
     * @param text
     */
    public static void setText(View v, CharSequence text){
        if (v == null){
            return;
        }
        if (TextUtils.isEmpty(text)){
            text = "";
        }
        if (v instanceof TextView){
            ((TextView)v).setText(text);
        }else if (v instanceof Button){
            ((Button)v).setText(text);
        }else if (v instanceof CheckBox){
            ((CheckBox)v).setText(text);
        }else if (v instanceof EditText){
            ((EditText)v).setText(text);
        }else if (v instanceof RadioButton){
            ((RadioButton)v).setText(text);
        }
    }


    /**
     * 说明：View赋值
     * @param v
     * @param resString
     */
    public static void setText(View v, @StringRes int resString){
        if (v != null){
            setText(v,v.getContext().getString(resString));
        }
    }

    /**
     * 说明：View赋值
     * @param v
     * @param format
     * @param obj
     */
    public static void setText(View v, String format, Object...obj){
        if (v != null){
            setText(v, String.format(format,obj));
        }
    }

    /**
     * 说明：View赋值
     * @param v
     * @param resFormat
     * @param obj
     */
    public static void setText(View v, @StringRes int resFormat, Object...obj){
        if (v != null){
            setText(v,String.format(v.getContext().getString(resFormat),obj));
        }
    }

    /**
     * 给TextView设置图片
     * @param tv
     * @param drawable
     * @param direction
     */
    public static void setTextDrawable(TextView tv, @DrawableRes int drawable, int direction){
        if (tv != null){
            Drawable d = tv.getContext().getResources().getDrawable(drawable);
            setTextDrawable(tv,d,direction);
        }
    }

    /**
     * 给TextView设置图片
     * @param tv
     * @param drawable
     * @param direction
     */
    public static void setTextDrawable(TextView tv,  Drawable drawable, int direction){
        if (tv != null){
            drawable.setBounds(0,0,drawable.getMinimumWidth(),drawable.getMinimumHeight());
            switch (direction){
                case Direction.Left:
                    tv.setCompoundDrawables(drawable,null,null,null);
                    break;
                case Direction.Right:
                    tv.setCompoundDrawables(null,null,drawable,null);
                    break;
                case Direction.Top:
                    tv.setCompoundDrawables(null,drawable,null,null);
                    break;
                case Direction.Bottom:
                    tv.setCompoundDrawables(null,null,null,drawable);
                    break;
            }
        }
    }
    /**
     * 说明：设置View显示和隐藏
     * @param view
     * @param show
     */
    public static void setVisibleOrGone(View view,boolean show){
        if (show){
            VISIBLE(view);
        }else {
            GONE(view);
        }
    }

    /**
     * 说明：设置View为GONE
     * @param view
     */
    public static void GONE(@Size(min = 1) View...view){
        if (view != null){
            for (View v:view){
                if (v != null){
                    if (v.getVisibility() != View.GONE){
                        v.setVisibility(View.GONE);
                    }
                }
            }
        }
    }

    /**
     * 说明：设置View为VISIBLE
     * @param view
     */
    public static void VISIBLE(@Size(min = 1) View...view){
        if (view != null){
            for (View v:view){
                if (v != null){
                    if (v.getVisibility() != View.VISIBLE){
                        v.setVisibility(View.VISIBLE);
                    }
                }
            }
        }
    }

    /**
     * 说明：设置View为INVISIBLE
     * @param view
     */
    public static void INVISIBLE(@Size(min = 1) View...view){
        if (view != null){
            for (View v:view){
                if (v != null){
                    if (v.getVisibility() != View.INVISIBLE){
                        v.setVisibility(View.INVISIBLE);
                    }
                }
            }
        }
    }

    /**
     * 说明：判断是否为隐藏
     * @param view
     * @return
     */
    public static boolean isHide(View view){
        return !isShow(view);
    }

    /**
     * 说明：判断是否显示
     * @param view
     * @return
     */
    public static boolean isShow(View view){
        boolean flag = false;
        if (view != null){
            flag = view.getVisibility() == View.VISIBLE;
        }
        return flag;
    }

    /**
     * View Enable
     * @param view
     * @param enable
     */
    public static void enable(View view, boolean enable){
        if (view != null){
            view.setEnabled(enable);
        }
    }

    /**
     * 关闭对话框
     * @param dialogs
     */
    public static void dismiss(Dialog...dialogs){
        if (dialogs != null && dialogs.length > 0){
            for (Dialog dialog:dialogs){
                if (dialog != null && dialog.isShowing()){
                    dialog.dismiss();
                }
            }
        }
    }

    /**
     * findViewById
     * @param activity
     * @param id
     * @return
     */
    public static <T extends View> T find(Activity activity, int id) {
        T t = (T) activity.findViewById(id);
        return t;
    }

    /**
     * findViewById
     * @param view
     * @param id
     * @return
     */
    public static <T extends View> T find(View view, int id) {
        return find(view,id,null);
    }

    /**
     * findViewById
     * @param view
     * @param id
     * @return
     */
    public static <T extends View> T find(View view, int id, View.OnClickListener listener) {
        T t = (T) view.findViewById(id);
        if (t != null && listener != null){
            t.setOnClickListener(listener);
        }
        return t;
    }

    /**
     * setOnClickListener
     * @param view
     * @param listener
     */
    public static void setOnClickListener(View view,View.OnClickListener listener){
        if (view != null && listener != null){
            view.setOnClickListener(listener);
        }
    }
}
